<?php

class Ctl_Gallery extends ControlBase {

    var $modul = "info";

    function Ctl_Gallery() {
// Constructor of father class.
	ControlBase::ControlBase();
    }

    function execute($action = "") {
	global $db;
	global $arrayError, $_POST;
	include_once DIR_MODEL . 'md_gallery.class.php';
	$objGallery = new MD_Gallery($db);
	$arrayData = array();
// Get back the work you want to do.
	$this->modul = CM_getParameter(MODUL);
	if ($action == "") {
// Get back the action: as: Add, Delete, Edit or List.
	    $action = CM_getParameter(ACTION);
	}

	switch ($action) {
	    case "gallery_home":
		include DIR_VIEW . 'v_gallery_home.php';
		$id = CM_getParameter('id');
		if (isset($_POST['Submit'])) {
		    if ($this->edit_pic_slide('gallery_home')) {
			echo "<script>alert('Cập nhật thành công!');</script>";
			$ulr = "?" . MODUL . "=" . $this->modul . "&action=gallery_home";
			CM_gotoPage($ulr);
		    } else {
			$arrayData = &$_POST;
			V_Gallery_Home($arrayData, "gallery_home", $id);
		    }
		} else if (isset($_POST['upload_gallery_home'])) {
		    if ($this->do_Upload_Gallery_home()) {
			echo "<script>alert('Upload thành công!');</script>";
			$ulr = "?" . MODUL . "=" . $this->modul . "&action=gallery_home";
			CM_gotoPage($ulr);
		    } else {
			$arrayData = &$_POST;
			V_Gallery_Home($arrayData, "gallery_home", $id);
		    }
		} else {
		    $arrayData = $objGallery->getAll("*", " WHERE type_id=0 ORDER BY id DESC");
		    V_Gallery_Home($arrayData, "gallery_home", $id);
		}
		break;
	    case "gallery_slide_home":
		include DIR_VIEW . 'v_gallery_slide_home.php';
		$id = CM_getParameter('id');
		if (isset($_POST['Submit'])) {
		    if ($this->edit_pic_slide()) {
			echo "<script>alert('Cập nhật thành công!');</script>";
			$ulr = "?" . MODUL . "=" . $this->modul . "&action=gallery_slide_home";
			CM_gotoPage($ulr);
		    } else {
			$arrayData = &$_POST;
			V_Gallery_Slide_Home($arrayData, "gallery_slide_home", $id);
		    }
		} else if (isset($_POST['upload_slide'])) {
		    if ($this->do_Upload_Slide_home()) {
			echo "<script>alert('Upload thành công!');</script>";
			$ulr = "?" . MODUL . "=" . $this->modul . "&action=gallery_slide_home";
			CM_gotoPage($ulr);
		    } else {
			$arrayData = &$_POST;
			V_Gallery_Slide_Home($arrayData, "gallery_slide_home", $id);
		    }
		} else {
		    $arrayData = $objGallery->getAll("*", " WHERE type_id IS NULL ORDER BY id DESC");
		    V_Gallery_Slide_Home($arrayData, "gallery_slide_home", $id);
		}
		break;
	    case "Edit_type":
		$this->do_edit_gallery_type();
		break;
	    case "add_gallery_type":
		$this->do_add_gallery_type();
		break;
	    case "Delete_type":
		$this->do_delete_type();
		break;
	    case "gallery_type":
		$arrayData = $objGallery->get_gallery_type_by_Where("", "", "");
		include DIR_VIEW . 'v_gallery_type.php';
		V_Gallery_Type($arrayData);
		break;
	    case "add_pic":
		$this->do_add_pic();
		break;
	    case "Edit_pic":
		$this->do_edit_pic();
		break;
	    case "Del_pic":
		$this->do_delete_pic();
		break;
	    case "List":
	    default :
		$parent_id = CM_getParameter('parent_id');
		if ($parent_id != "") {
		    $listField = "*";
		    $where = "type_id='$parent_id'";
		    $order = " id DESC";
		    $arrayData = $objGallery->getByWhere($listField, $where, $order);
		    $where1 = " id = '$parent_id'";
		    $order1 = "";
		    $limit1 = "";
		    $arrayData_Gallery_type = $objGallery->get_gallery_type_by_Where_1($where1, $order1, $limit1);
		} else {
		    $arrayData = $objGallery->getAll("*", " WHERE type_id != '0' ORDER BY id DESC");
		    $arrayData_Gallery_type = "";
		}
		include_once DIR_VIEW . 'v_gallery.php';
		V_Gallery($arrayData, $arrayData_Gallery_type);
		break;
	}

	unset($arrayData);
    }

    function do_Upload_Slide_home() {
	global $arrayError, $_POST, $db;
	$objGallery = new MD_Gallery($db);
	$field_name = 'url_pic_up';
	$dirupload = DIR_SLIDER;
	$this->upload_Multifile($field_name, $dirupload);
	$objGallery->upload_slide_pic();
	if ($objGallery->errno()) {
	    $arrayError[] = $objGallery->error();
	    return false;
	}
	unset($_SESSION['ARRAY_IMG']);
	return true;
    }

    function do_Upload_Gallery_home() {
	global $arrayError, $_POST, $db;
	$objGallery = new MD_Gallery($db);
	$field_name = 'url_pic_up';
	$dirupload = DIR_IMG_SLIDE_GALLERY_HOME;
	$this->upload_Multifile($field_name, $dirupload);
	$objGallery->upload_slide_pic('0');
	if ($objGallery->errno()) {
	    $arrayError[] = $objGallery->error();
	    return false;
	}
	unset($_SESSION['ARRAY_IMG']);
	return true;
    }

    function do_delete_pic() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objGallery = new MD_Gallery($db);

	$id = CM_getParameter("id");
	$action = CM_getParameter("flag");
	if ($this->delete_pic($id)) {
	    echo "<script>alert('Xóa ảnh thành công!');</script>";
	    $ulr = "?" . MODUL . "=" . $this->modul;
	    if ($action != "") {
		$ulr .= "&action=" . $action;
	    }
	    CM_gotoPage($ulr);
	} else {
	    $arrayError[] = " Có lỗi trong quá trình xóa dữ liệu.";
	}
    }

    function delete_pic() {
	global $arrayError, $_POST, $db;
	$objGallery = new MD_Gallery($db);
	$id = CM_getParameter("id");
	$dirname = DIR_IMG_SLIDE_GALLERY;

	$action = CM_getParameter("flag");
	if ($action == "gallery_slide_home") {
	    $dirname = DIR_SLIDER;
	} else if ($action == "gallery_home") {
	    $dirname = DIR_IMG_SLIDE_GALLERY_HOME;
	}

	$arrayCheck = $objGallery->getById2($id);
	if ($arrayCheck['url_pic'] != '') {
	    unlink($dirname . $arrayCheck['url_pic']);
	}
	$objGallery->delete_pic($id);
	if ($objGallery->errno()) {
	    $arrayError[] = $objGallery->error();
	    return false;
	}
	return true;
    }

    function do_edit_pic() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objGallery = new MD_Gallery($db);

	include_once DIR_VIEW . 'v_add_gallery.php';

	if (isset($_POST['Submit'])) {
	    if ($this->edit_pic()) {
		echo "<script>alert('Cập nhật thành công!');</script>";
		$ulr = "?" . MODUL . "=" . $this->modul;
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Gallery($arrayData, "Edit_gallery");
	    }
	} else {
	    $id = CM_getParameter("id");
	    $where1 = " id = '$id'";
	    $order1 = "";
	    $limit1 = "";
	    $arrayData = $objGallery->getById2($id, "*");
	    V_Add_Gallery($arrayData, "Edit_pic");
	}
    }

    function edit_pic() {
	global $arrayError, $_POST, $db;
	$objGallery = new MD_Gallery($db);
	$id = CM_getParameter("id");
	$dirname = DIR_IMG_SLIDE_GALLERY;
	if ($_FILES["url_pic"]['name'] != "") {
	    $arrayCheck = $objGallery->getById2($id);
	    if ($arrayCheck['url_pic'] != '') {
		unlink($dirname . $arrayCheck['url_pic']);
	    }
	    $this->DoUploadFileNews_Gallery($dirname);
	}
	$objGallery->edit_pic(&$_POST);
	if ($objGallery->errno()) {
	    $arrayError[] = $objGallery->error();
	    return false;
	}
	unset($_SESSION['Pkd_images2']);
	return true;
    }

    function edit_pic_slide($action) {
	global $arrayError, $_POST, $db;
	$objGallery = new MD_Gallery($db);
	$id = CM_getParameter("id");
	$dirname = DIR_SLIDER;
	if ($action == "gallery_slide_home") {
	    $dirname = DIR_SLIDER;
	} else if ($action == "gallery_home") {
	    $dirname = DIR_IMG_SLIDE_GALLERY_HOME;
	}
	if ($_FILES["url_pic"]['name'] != "") {
	    $arrayCheck = $objGallery->getById2($id);
	    if ($arrayCheck['url_pic'] != '') {
		unlink($dirname . $arrayCheck['url_pic']);
	    }
	    $this->DoUploadFileNews_Gallery($dirname);
	}
	$objGallery->edit_pic_slide(&$_POST);
	if ($objGallery->errno()) {
	    $arrayError[] = $objGallery->error();
	    return false;
	}
	unset($_SESSION['Pkd_images2']);
	return true;
    }

    function do_add_pic() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objGallery = new MD_Gallery($db);

	include_once DIR_VIEW . 'v_add_gallery.php';

	if (isset($_POST['Submit'])) {
	    if ($this->add_pic()) {
		echo "<script>alert('Thêm mới thành công!');</script>";
		$ulr = "?" . MODUL . "=" . $this->modul;
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Gallery($arrayData, "add_pic");
	    }
	} else {
	    $id = CM_getParameter("id");
	    $where1 = " id = '$id'";
	    $order1 = "";
	    $limit1 = "";
	    $arrayData = $objGallery->getById2($id, "*");
	    V_Add_Gallery($arrayData, "add_pic");
	}
    }

    function add_pic() {
	global $arrayError, $_POST, $db;
	$objGallery = new MD_Gallery($db);
	$dirname = DIR_IMG_SLIDE_GALLERY;
	if (count($_FILES['url_pic']['name']) <= 6) {

	    for ($i = 0; $i < count($_FILES['url_pic']['name']); $i++) {
		if ($_FILES['url_pic']['name'][$i] != "") {
		    $ten_anh = time() . "_" . $_FILES['url_pic']['name'][$i];
		    $ten_file_t = $this->utf8convert($ten_anh);
		    $ten_file = $this->utf8tourl($ten_file_t);
		    move_uploaded_file($_FILES['url_pic']['tmp_name'][$i], $dirname . $ten_file);
		    $this->resize_images_new($dirname, $ten_file, $ten_file, '900', '900');
		    $_SESSION['Pkd_images2'] = $ten_file;
		    $objGallery->add_pic(&$_POST);
		    unset($_SESSION['Pkd_images2']);
		}
	    }
	}
	if ($objGallery->errno()) {
	    $arrayError[] = $objGallery->error();
	    return false;
	}
	return true;
    }

    function do_delete_type() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objGallery = new MD_Gallery($db);

	$id = CM_getParameter("id");
	if ($this->delete_type($id)) {
	    echo "<script>alert('Xóa thành công!');</script>";
	    $ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=gallery_type";
	    CM_gotoPage($ulr);
	} else {
	    $arrayError[] = " Có lỗi trong quá trình xóa dữ liệu.";
	}
    }

    function delete_type() {
	global $arrayError, $_POST;
	$objGallery = new MD_Gallery($this->db);
	$id = CM_getParameter("id");
	$objGallery->delete_type($id);
	if ($objGallery->errno()) {
	    $arrayError[] = $objGallery->error();
	    return false;
	}
	return true;
    }

    function do_edit_gallery_type() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objGallery = new MD_Gallery($db);

	include_once DIR_VIEW . 'v_add_gallery_type.php';

	if (isset($_POST['Submit'])) {
	    if ($this->update_type()) {
		echo "<script>alert('Cập nhật thành công!');</script>";
		$ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=gallery_type";
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Gallery_Type($arrayData, "Edit_type");
	    }
	} else {
	    $id = CM_getParameter("id");
	    $where1 = " id = '$id'";
	    $order1 = "";
	    $limit1 = "";
	    $arrayData = $objGallery->get_gallery_type_by_Where_1($where1, $order1, $limit1);
	    V_Add_Gallery_Type($arrayData, "Edit_type");
	}
    }

    function update_type() {
	global $arrayError, $_POST, $db;
	$objGallery = new MD_Gallery($db);
	$objGallery->update_type(&$_POST);
	if ($objGallery->errno()) {
	    $arrayError[] = $objGallery->error();
	    return false;
	}
	return true;
    }

    function do_add_gallery_type() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objGallery = new MD_Gallery($db);

	include_once DIR_VIEW . 'v_add_gallery_type.php';

	if (isset($_POST['Submit'])) {
	    if ($this->insert_type()) {
		echo "<script>alert('Thêm mới thành công!');</script>";
		$ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=gallery_type";
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Gallery_Type($arrayData, "add_gallery_type");
	    }
	} else {
	    V_Add_Gallery_Type($arrayData, "add_gallery_type");
	}
    }

    function insert_type() {
	global $arrayError, $_POST, $db;
	$objGallery = new MD_Gallery($db);
	$objGallery->insert_type(&$_POST);
	if ($objGallery->errno()) {
	    $arrayError[] = $objGallery->error();
	    return false;
	}
	return true;
    }

}

?>